package com.sense.kukoo.biz_messages.view

import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.gyf.immersionbar.ImmersionBar
import com.sense.kukoo.biz_messages.R
import com.sense.kukoo.biz_messages.adapter.MessagesItemAdapter
import com.sense.kukoo.biz_messages.databinding.BizMessagesActivityMessagesCommentsBinding
import com.sense.kukoo.biz_messages.vm.MessagesCommentsViewModel
import com.sense.kukoo.common.anim.BottomMotionAnimHelper
import com.sense.kukoo.common.constant.ARouterUrl
import com.sense.kukoo.common.mvvm.view.BaseMvvmActivity
import com.sense.kukoo.common.repo.biz.MessagesItemBean
import com.sense.kukoo.common.repo.http.bean.UserMessageCommentsBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.state.NetworkErrorState
import com.zy.multistatepage.state.SuccessState

@Route(path = ARouterUrl.ROUTE_MESSAGES_MODULE_MESSAGES_COMMENTS_ACTIVITY)
class MessagesCommentsActivity : BaseMvvmActivity<MessagesCommentsViewModel, BizMessagesActivityMessagesCommentsBinding>() {

    private val TAG: String = javaClass.simpleName
    private val mAnimHelper = BottomMotionAnimHelper()

    private lateinit var mAdapter: MessagesItemAdapter

    @JvmField
    @Autowired(name = ARouterUrl.KEY_FROM)
    var from: String? = null

    override fun responseVM(action: Int) {}

    override fun getLayoutId(): Int = R.layout.biz_messages_activity_messages_comments

    override fun getViewModelClass(): Class<MessagesCommentsViewModel> = MessagesCommentsViewModel::class.java

    override fun initStatusBar() {
        ImmersionBar.with(this)
            .transparentStatusBar()
            .navigationBarColor(com.sense.kukoo.common_design.R.color.bg_background)
            .statusBarDarkFont(true)
            .navigationBarDarkIcon(true)
            .fitsSystemWindows(false)
            .init()
    }

    override fun initData() {
        ARouter.getInstance().inject(this)
        mViewModel.setNewsNum(from?.toInt() ?: 0)
        //
        mBinding.lifecycleOwner = this
        mBinding.vm = mViewModel
        //
        bindAction(mViewModel.action_back){
            mAnimHelper.animateToFinish()
        }
        bindAction(mViewModel.action_networkError){
            mBinding.multiStateContainer.show<NetworkErrorState> {
                it.onRetry {
                    mViewModel.onNetworkRetry()
                }
            }
        }
        bindAction(mViewModel.action_finishRefresh){
            mBinding.refreshLayout.finishRefresh()
        }
        bindData(mViewModel.data_comments){
            mBinding.multiStateContainer.show<SuccessState>()
            mAdapter.list = it
            mAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        //
        mAdapter = MessagesItemAdapter(arrayListOf(), mListener)
        mBinding.recyclerView.adapter = mAdapter

        val linearLayoutManager = LinearLayoutManager(this)
        mBinding.recyclerView.layoutManager = linearLayoutManager
        mBinding.refreshLayout.setOnRefreshListener {
            //OnRefresh
            mViewModel.refreshCommentsData()
        }
        //弹出动画
        val first = ApplicationContext.dp2px(800F).toFloat()
        mAnimHelper.init(mBinding.flBgRoot, mBinding.clContentRoot, first){ finish() }
        mAnimHelper.animateToStart()
        //
        mBinding.refreshLayout.autoRefresh()
    }

    override fun onBackPressed() {
        mAnimHelper.animateToFinish()
    }

    private val mListener = object : MessagesItemAdapter.MessagesItemListener {
        override fun onViewMore(data: MessagesItemBean) {
            mViewModel.loadMoreCommentsData()
        }

        override fun onDeleteItem(data: MessagesItemBean) {
            val comment: UserMessageCommentsBean = data.comment!!
            mViewModel.deleteComment(comment)
        }

        override fun onClickMusic(data: MessagesItemBean) {
            val comment: UserMessageCommentsBean = data.comment!!
            ARouter.getInstance()
                .build(ARouterUrl.ROUTE_MEDIA_MODULE_MEDIA_DESCRIPTION_ACTIVITY)
                .withString(ARouterUrl.KEY_FROM, "${comment.contentId}")
                .navigation()
        }

        override fun onClickChannel(data: MessagesItemBean) {}

        override fun onClickFollow(data: MessagesItemBean) {}
    }
}